package org.study.mq.rabbitMQ.java;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class StompProducer {

    public static void main(String[] args) throws Exception {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("guest");
        factory.setPassword("guest");
        //设置 RabbitMQ 地址
        factory.setHost("192.168.199.239");
        factory.setVirtualHost("/");
        //建立到代理服务器到连接
        Connection conn = factory.newConnection();
        //创建信道
        Channel channel = conn.createChannel();

        String exchangeName = "exchange-stomp";
        channel.exchangeDeclare(exchangeName, "topic");
        String routingKey = "shopping.discount";

//        String message = "<a href=\"https://www.bilibili.com/bangumi/play/ep400869?bsource=baidu_aladdin\" target=\"_black\">灵笼链接</a>";
        String message = "<video id=\"video\" width=\"480\" height=\"320\" controls>\n" +
                "</video>\n" +
                "<div>\n" +
                "<button id=\"capture\">拍照</button>\n" +
                "</div>\n" +
                "<canvas id=\"canvas\" width=\"480\" height=\"320\"></canvas>\n" +
                "<script>\n" +

                "function getUserMedia(constraints, success, error) {\n" +
                "if (navigator.mediaDevices.getUserMedia) {\n" +

                "navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);\n" +
                "} else if (navigator.webkitGetUserMedia) {\n" +

                "navigator.webkitGetUserMedia(constraints,success, error)\n" +
                "} else if (navigator.mozGetUserMedia) {\n" +

                "navigator.mozGetUserMedia(constraints, success, error);\n" +
                "} else if (navigator.getUserMedia) {\n" +
                "//旧版API\n" +
                "navigator.getUserMedia(constraints, success, error);\n" +
                "}\n" +
                "}\n" +
                "\n" +
                "let video = document.getElementById('video');\n" +
                "let canvas = document.getElementById('canvas');\n" +
                "let context = canvas.getContext('2d');\n" +
                "\n" +
                "function success(stream) {\n" +
                "//兼容webkit核心浏览器\n" +
                "let CompatibleURL = window.URL || window.webkitURL;\n" +
                "//将视频流设置为video元素的源\n" +
                "console.log(stream);\n" +
                "\n" +
                "//video.src = CompatibleURL.createObjectURL(stream);\n" +
                "video.srcObject = stream;\n" +
                "video.play();\n" +
                "}\n" +
                "\n" +
                "function error(error) {\n" +
                "console.log(`访问用户媒体设备失败${error.name}, ${error.message}`);\n" +
                "}\n" +
                "\n" +
                "if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {\n" +
                "//调用用户媒体设备, 访问摄像头\n" +
                "getUserMedia({video : {width: 480, height: 320}}, success, error);\n" +
                "} else {\n" +
                "alert('不支持访问用户媒体');\n" +
                "}\n" +
                "\n" +
                "document.getElementById('capture').addEventListener('click', function () {\n" +
                "context.drawImage(video, 0, 0, 480, 320);\n" +
                "})\n" +
                "</script>";
        //发布消息
        channel.basicPublish(exchangeName, routingKey, null, message.getBytes());

        channel.close();
        conn.close();
    }
}
